package com.dge.app.providers;

import com.dge.app.ResourceTable;
import com.dge.app.utils.ResourceUtils;
import com.dge.app.utils.SizeUtils;
import ohos.aafwk.ability.AbilitySlice;
import ohos.agp.components.*;
import ohos.agp.components.element.PixelMapElement;
import ohos.global.resource.Resource;

import java.util.ArrayList;

public class FunctionListProvider extends RecycleItemProvider {
    private ArrayList<String> items;
    private AbilitySlice slice;

    public FunctionListProvider(ArrayList<String> dataItemList, AbilitySlice slice) {
        this.items = dataItemList;
        this.slice = slice;
    }
    public ArrayList<String> getItems() {
        return items;
    }

    @Override
    public int getCount() {
        return items.size();
    }

    @Override
    public Object getItem(int i) {
        return items.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public Component getComponent(int i, Component component, ComponentContainer componentContainer) {
        Component itemComponent = component;
        TextHolder viewHolder;
        if (itemComponent == null) {
            itemComponent = LayoutScatter.getInstance(slice)
                    .parse(ResourceTable.Layout_item_function, componentContainer, false);
            int screenWidth = SizeUtils.getScreenWidth();
            int width = (screenWidth - SizeUtils.vp2px(4)) / 5;
            ComponentContainer.LayoutConfig layoutConfig = new ComponentContainer.LayoutConfig(width, ComponentContainer.LayoutConfig.MATCH_CONTENT);
            itemComponent.setLayoutConfig(layoutConfig);
            viewHolder = new TextHolder(itemComponent);
            itemComponent.setTag(viewHolder);
        } else {
            viewHolder = (TextHolder) itemComponent.getTag();
        }
        String item = items.get(i);

        boolean isAdd = "更多".equals(item) && items.size() < 10;
        if (!isAdd) viewHolder.text.setText(item);
        Integer imgId = ResourceUtils.nameResourceMap.get(item);
        if (imgId == null) {
            imgId = ResourceTable.Media_ic_a_battle;
        }
        Resource resource = null;
        try {
            resource = slice.getResourceManager().getResource(isAdd ? ResourceTable.Media_ic_add : imgId);
            PixelMapElement element = new PixelMapElement(resource);
            if (element != null) {
                int size = SizeUtils.vp2px(40);
                element.setBounds(0, 0, size, size);
                viewHolder.text.setAroundElements(null, element, null, null);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return itemComponent;
    }

    public static class TextHolder {
        Text text;
        Image ivDelete;

        TextHolder(Component itemComponent) {
            text = itemComponent.findComponentById(ResourceTable.Id_tvTitle);
            ivDelete = itemComponent.findComponentById(ResourceTable.Id_ivDelete);
        }
    }
}